package org.onvivals.module.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.onvivals.module.system.entity.SysMenu;
import org.onvivals.module.system.entity.SysRole;
import org.onvivals.module.system.entity.SysRoleMenu;
import org.onvivals.module.system.entity.SysUserRole;
import org.onvivals.module.system.mapper.SysMenuMapper;
import org.onvivals.module.system.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService{

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> listMenuByUserId(Long userId, Integer menuType) {
        MPJLambdaWrapper<SysMenu> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(SysMenu.class)
                .innerJoin(SysRoleMenu.class, SysRoleMenu::getMenuId, SysMenu::getMenuId)
                .innerJoin(SysUserRole.class, SysUserRole::getRoleId, SysRoleMenu::getRoleId)
                .innerJoin(SysRole.class, SysRole::getRoleId, SysUserRole::getRoleId)
                .eq(SysUserRole::getUserId, userId);
        if (menuType != null) {
            wrapper.eq(SysMenu::getMenuType, menuType);
        }
        wrapper.orderByAsc(SysMenu::getSortNumber);
        return sysMenuMapper.selectJoinList(SysMenu.class, wrapper);
    }
}
